package com.goldman.base.common.sys.exception;


import com.goldman.http.common.s2s.model.IErrorCode;

import java.text.MessageFormat;

/**
 * 通用业务运行时异常
 */
public class BusinessException extends RuntimeException {

    private Integer code;
    private String msg;
    private String error;
    private IErrorCode errorCode;
    private Object[] args;

    public BusinessException(IErrorCode errorCode) {
        super(errorCode.getMsg());
        this.errorCode = errorCode;
        this.code = errorCode.getCode();
        this.msg = errorCode.getMsg();
    }

    public BusinessException(IErrorCode errorCode, String error) {
        super(String.format("%s:%s", errorCode.getMsg(), error));
        this.errorCode = errorCode;
        this.code = errorCode.getCode();
        this.msg = errorCode.getMsg();
        this.error = error;
    }

    public BusinessException(IErrorCode errorCode, String error, Throwable cause) {
        super(String.format("%s:%s", errorCode.getMsg(), error), cause);
        this.errorCode = errorCode;
        this.code = errorCode.getCode();
        this.msg = errorCode.getMsg();
        this.error = error;
    }


    public BusinessException(IErrorCode errorCode, Throwable cause) {
        super(cause);
        this.errorCode = errorCode;
        this.code = errorCode.getCode();
        this.msg = errorCode.getMsg();
    }

    public BusinessException(IErrorCode errorCode, Throwable cause, Object... args) {
        super(cause);
        this.code = errorCode.getCode();
        this.msg = MessageFormat.format(errorCode.getMsg(), args);
    }

    public BusinessException(Integer code, String msg) {
        super(msg);
        this.code = code;
        this.msg = msg;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public IErrorCode getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(IErrorCode errorCode) {
        this.errorCode = errorCode;
    }

    public Object[] getArgs() {
        return args;
    }

    public void setArgs(Object[] args) {
        this.args = args;
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }
}
